import router from "@/router";

export default {
  state: {
    taglist: [{ name: "首页", path: "/home/mainpage", bool: true, kela: 0 }],
  },
  getters: {
    //拿到上面数组直接返回
    taglist(state) {
      return state.taglist;
    },
  },
  mutations: {
    //追加标签
    addTag(state, index) {
      // console.log(index);  //监听传过来的全部路由数据
      let nameRoute = router.currentRoute._value.name; //拿到当前位置的name路由
      // console.log(nameRoute);
      if (index.matched.length > 2) {
        let prd = state.taglist.findIndex((item) => item.name == "商品列表");
        // console.log(prd);
        let dala = state.taglist.findIndex((item) => item.path == index.path);
        // console.log(dala);  //
        if (dala == -1) {
          //没找到就添加标签数据
          if (nameRoute == "editor" || nameRoute == "commod") {
            //判断路由是添加还是编辑
            prd == -1 ? (prd = 1) : prd;
            state.taglist.splice(prd + 1, 0, {
              name: index.meta.title,
              path: index.path,
            });
          } else {
            state.taglist.push({
              name: index.meta.title,
              path: index.path,
            });
          }
        }
      } else {
        // console.log("没找到了不添加！");
      }
      state.index = state.taglist.findIndex(
        (val) => val.name == index.meta.title
      );
      // console.log(state.index);
    },
    //删除
    handleClose(state, index) {
      //下标
      // state.taglist

      state.taglist.splice(index, 1);
      if (index == state.taglist.length) {
        router.push({ path: state.taglist[index - 1].path });
      } else {
        router.push({ path: state.taglist[index].path });
      }
    },
    // //关闭页面
    // closeTag({ taglist }, rou) {

    //  let index = taglist.findIndex(item =>  item.name == rou);

    //   taglist.splice(index, 1);
    // },
    //点击标签跳转
    arrs(state, index) {
      // state.kela = index;
      state.taglist.find((item) => item.path == index);
      router.push(index);
    },

    //全部刪除
    allDown(state) {
      console.log(state);
      state.taglist.splice(1, state.taglist.length + 1);
      router.push("/home/mainpage");
    },
    //右侧关闭
    rightDown({ taglist }, index) {
      if (taglist.length - 1 > index) {
        let num = taglist.length - 1 - index;
        taglist.splice(index + 1, num);
        router.push(taglist[index].path);
      }
    },
  },
  actions: {
    addTag(context, index) {
      context.commit("addTag", index);
    },
    handleClose(context, index) {
      if (typeof index != "number") {
        // console.log(index);
        var v = context.state.taglist.findIndex((item) => item.path == index);
        context.commit("handleClose", v);
        return;
      }
      context.commit("handleClose", index);
    },
    //关闭页面
    closeTag(context, rou) {
      context.commit("closeTag", rou);
    },
    arrs(context, index) {
      context.commit("arrs", index);
    },
    allDown(context, index) {
      context.commit("allDown", index);
    },
    rightDown(context, index) {
      context.commit("rightDown", index);
    },
  },
};
